package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {
    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IAppointmentService appointmentService;
    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] Ids = Convert.toLongArray(ids);
        for (Long id : Ids) {
            statementItemMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    @Transactional
    public void saveItems(List<StatementItem> items) {
        StatementItem tempItem = items.remove(items.size() - 1);
        BigDecimal disCountPrice = tempItem.getItemPrice();
        Long statementId = tempItem.getStatementId();
        Statement statement = statementService.get(statementId);
        if (statement.getStatus().equals(Statement.STATUS_PAID)){
            throw new BusinessException("已经支付的结算单不能保存明细");
        }
        statementItemMapper.delateBystatementId(statementId);
        BigDecimal totalAmount = new BigDecimal("0.0");
        BigDecimal totalQuantity = new BigDecimal("0");
        for (StatementItem item : items) {
            statementItemMapper.insert(item);
            totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
            totalQuantity = totalQuantity.add(item.getItemQuantity());
        }
        if (disCountPrice.compareTo(totalAmount)>0){
            throw new BusinessException("非法操作,折扣金额不应该大于总金额");
        }
        statementService.updateAmount(statementId,totalAmount,totalQuantity,disCountPrice);
    }

    @Override
    public void payStatement(Long statementId) {
        Statement statement = statementService.get(statementId);
        if (statement.getStatus().equals(Statement.STATUS_PAID)){
            throw new BusinessException("已经支付了请不要重复支付");
        }
        //更新结算单的状态
        statementService.updatePayStatus(statementId,Statement.STATUS_PAID,ShiroUtils.getUserId());
        if (statement.getAppointmentId()!=null){
            appointmentService.updateStatus(statement.getAppointmentId(), Appointment.STATUS_SETTLE);
        }
    }

    @Override
    public void removeByStatementId(Long statementId) {
        statementItemMapper.delateBystatementId(statementId);
    }


}
